home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / graphics / qrt.lzh / STACK.C < prev    next >
C/C++ Source or Header  |  1989-02-16  |  9KB  |  343 lines

  1. /*******************************************************
  2.  
  3.                   stacks and queues
  4.  
  5.  *******************************************************/
  6.  
  7. #include "qrt.h"
  8. #include "pattern.h"
  9.  
  10. /* #define STACKDEBUG 1 */
  11.  
  12. char *malloc();
  13.  
  14. /**********************************************************
  15.  
  16.     Calls the pre-computing routine for each object in
  17.     the object tree.  The pre-computing routines figure
  18.     out some sub-expressions that don't change with
  19.     different calls to the intersection routines.
  20.  
  21.  **********************************************************/
  22.  
  23. Do_Precomp(node)
  24.   OBJ_PTR node;
  25. {
  26.  
  27. # ifdef STACKDEBUG
  28.     printf("DOPRECOMP: type = %d\n", node->type);
  29. # endif
  30.  
  31.   (*(ObjData[node->type].PreComp))(node);  /* precompute */
  32.  
  33.   if (node->child != NULL) {               /* node has children ? */
  34.  
  35. #   ifdef ROBUST
  36.       if (node->type != BBOX) Error(INTERNAL_ERROR,802);
  37. #   endif
  38.  
  39.     Do_Precomp(node->child);
  40.   }
  41.  
  42.   if (node->nextobj != NULL) {
  43.     Do_Precomp(node->nextobj);
  44.   }
  45. }
  46.  
  47. /**********************************************************
  48.  
  49.     Assigns bounding box values for entire object tree.
  50.     This must be called once before any ray-tracing is done.
  51.  
  52.  **********************************************************/
  53.  
  54. Make_Bbox(node)
  55.   OBJ_PTR node;
  56. {
  57.   OBJ_PTR tnode;
  58.   VECTOR  v1,v2;
  59.  
  60. # ifdef STACKDEBUG
  61.     printf("MAKEBBOX\n");
  62. # endif
  63.  
  64.   if (node->child!=NULL) {                 /* node has children ? */
  65.  
  66. #   ifdef ROBUST
  67.       if (node->type!=BBOX) Error(INTERNAL_ERROR,801);
  68. #   endif
  69.  
  70.     Make_Bbox(node->child);
  71.   }
  72.  
  73.   if (node->nextobj!=NULL) {               /* node has siblings ? */
  74.     Make_Bbox(node->nextobj);
  75.   }
  76.  
  77.   if (node->type==BBOX) {
  78.     tnode=node->child;
  79.  
  80.     node->lower.x=node->lower.y=node->lower.z=  3e30;
  81.     node->upper.x=node->upper.y=node->upper.z= -3e30;
  82.  
  83.     while (tnode!=NULL) {
  84.       (*(ObjData[tnode->type].FindBbox))(&v1,&v2,tnode);
  85.       node->lower.x=MIN(node->lower.x,v1.x);
  86.       node->lower.y=MIN(node->lower.y,v1.y);
  87.       node->lower.z=MIN(node->lower.z,v1.z);
  88.  
  89.       node->upper.x=MAX(node->upper.x,v2.x);
  90.       node->upper.y=MAX(node->upper.y,v2.y);
  91.       node->upper.z=MAX(node->upper.z,v2.z);
  92.  
  93. #     ifdef STACKDEBUG
  94.         printf("MAKEBBOX: v1 x,y,z = %f %f %f\n",
  95.                node->lower.x,node->lower.y,node->lower.z);
  96.         printf("          v2 x,y,z = %f %f %f\n",
  97.                node->upper.x,node->upper.y,node->upper.z);
  98. #     endif
  99.  
  100.       tnode=tnode->nextobj;
  101.     }
  102.   }
  103. }
  104.  
  105.  
  106. /**********************************************************
  107.  
  108.     Returns pointer to pattern structure given name, or
  109.     null if not found.
  110.  
  111.  **********************************************************/
  112.  
  113. PATTERN_PTR find_pat(name)
  114.   char *name;
  115. {
  116.   PATTERN_PTR pat;
  117.  
  118.   pat=THEWORLD.patlist;
  119.  
  120.   while (pat!=NULL) {
  121.     if (strcmp(name,pat->name)==0) return(pat);
  122.     pat=pat->sibling;
  123.   }
  124.  
  125.   return(NULL);
  126. }
  127.  
  128.  
  129. /**********************************************************
  130.  
  131.      Allocates a new pattern structure and returns a
  132.      pointer to it.
  133.  
  134.  **********************************************************/
  135.  
  136. PATTERN_PTR new_pat()
  137. {
  138.   PATTERN_PTR pat;
  139.  
  140.   if ((pat=(PATTERN_PTR)malloc(sizeof(PATTERN)))==NULL)
  141.     Error(MALLOC_FAILURE,802);
  142.  
  143.   pat->name    = NULL;
  144.   pat->child   =
  145.   pat->sibling =
  146.   pat->link    = NULL;
  147.  
  148.   return(pat);
  149. }
  150.  
  151.  
  152. /**********************************************************
  153.  
  154.      Allocates a new object structure, stuffs most of
  155.      its information fields, and returns a pointer to it.
  156.  
  157.  **********************************************************/
  158.  
  159. OBJ_PTR new_obj(type,loc,v1,v2,v3,cinfo,pattern,remove,name,
  160.                 upper, lower, cterm, xmult, ymult)
  161.  
  162.         VECT_PTR     loc, v1, v2, v3, upper, lower;
  163.         CINFO_PTR    cinfo;
  164.         PATTERN_PTR  pattern, remove;
  165.         float        cterm, xmult, ymult;
  166.         char         *name;
  167. {
  168.   OBJ_PTR obj;
  169.   if ((obj=(OBJ_PTR)malloc(sizeof(OBJ_STRUCT)))==NULL)
  170.     Error(MALLOC_FAILURE,803);
  171.  
  172.   obj->type=type;                            /* copy info */
  173.   VectEQ(&(obj->loc),loc);
  174.   VectEQ(&(obj->vect1),v1);
  175.   VectEQ(&(obj->vect2),v2);
  176.   VectEQ(&(obj->vect3),v3);
  177.   VectEQ(&(obj->lower),lower);
  178.   VectEQ(&(obj->upper),upper);
  179.  
  180.   obj->cterm = cterm;
  181.   obj->xmult = xmult;
  182.   obj->ymult = ymult;
  183.  
  184.   obj->name = name;
  185.  
  186.   copy_colorinfo(&(obj->cinfo),cinfo);       /* colorinfo */
  187.  
  188.   obj->nextobj = obj->child = NULL;          /* no relatives */
  189.   obj->pattern = pattern;
  190.   obj->remove  = remove;
  191.  
  192.   return(obj);
  193. }
  194.  
  195.  
  196. /**********************************************************
  197.  
  198.                 Generates an empty line
  199.  
  200.  **********************************************************/
  201.  
  202. OBJ_PTR new_line() {
  203.   CINFO   cinfo;
  204.   VECTOR  loc,v1,v2,v3, upper, lower;
  205.   OBJ_PTR line;
  206.  
  207.   VectEqZero(&loc);
  208.   VectEqZero(&v1);
  209.   VectEqZero(&v2);
  210.   VectEqZero(&v3);
  211.   VectEqZero(&upper);
  212.   VectEqZero(&lower);
  213.  
  214.   line=new_obj(LINE,&loc,&v1,&v2,&v3,&cinfo,NULL,NULL,NULL,
  215.                &upper,&lower,(float)0.0,(float)0.0,(float)0.0);
  216.  
  217.   line->flag = FALSE;
  218.   return(line);
  219. }
  220.  
  221.  
  222. /**********************************************************
  223.  
  224.                  Adds a lamp to the world
  225.  
  226.  **********************************************************/
  227.  
  228. add_lamp(object)                   /* add a lamp to the world */
  229.   OBJ_PTR object;
  230. {
  231.    object->nextobj=THEWORLD.lamps;
  232.    THEWORLD.lamps=object;
  233. }
  234.  
  235.  
  236. /**********************************************************
  237.  
  238.        Debugging routine to print object structure
  239.                  MAY BE WRONG BY NOW
  240.  
  241.  **********************************************************/
  242.  
  243. #ifdef STACKDEBUG
  244.  
  245.   print_obj(obj)                     /* display object */
  246.     OBJ_PTR obj;
  247.   {
  248.     printf("OBJECT :  type: ");
  249.     switch (obj->type) {
  250.       case LINE:           printf("LINE\n");          break;
  251.       case SPHERE:         printf("SPHERE\n");        break;
  252.       case PARALLELOGRAM:  printf("PARALLELOGRAM\n"); break;
  253.       case TRIANGLE:       printf("TRIANGLE\n");      break;
  254.       case LAMP:           printf("LAMP\n");          break;
  255.       case OBSERVER:       printf("OBSERVER\n");      break;
  256.       case GROUND:         printf("GROUND\n");        break;
  257.       case SKY:            printf("SKY\n");           break;
  258.       case BBOX:           printf("BBOX\n");          break;
  259.       case RING:           printf("RING\n");          break;
  260.       case QUADRATIC:      printf("QUADRATIC\n");     break;
  261.       default:             printf("Unknown!\n");      break;
  262.     }
  263.  
  264.     printf("          loc   : %f, %f, %f\n",
  265.            (obj->loc.x),
  266.            (obj->loc.y),
  267.            (obj->loc.z));
  268.  
  269.     printf("          vect1 : %f, %f, %f\n",
  270.            (obj->vect1.x),
  271.            (obj->vect1.y),
  272.            (obj->vect1.z));
  273.  
  274.     printf("          vect2 : %f, %f, %f\n",
  275.            (obj->vect2.x),
  276.            (obj->vect2.y),
  277.            (obj->vect2.z));
  278.  
  279.     printf("          vect3 : %f, %f, %f\n",
  280.            (obj->vect3.x),
  281.            (obj->vect3.y),
  282.            (obj->vect3.z));
  283.  
  284.   }
  285. #endif
  286.  
  287.  
  288. /**********************************************************
  289.  
  290.             Prints some interesting statistics
  291.  
  292.  **********************************************************/
  293.  
  294. World_Stats() {
  295.   printf("World Statistics:\n");
  296.   printf("  Objects:      %4d\n",THEWORLD.objcount);
  297.   printf("  Lamps:        %4d\n",THEWORLD.lampcount);
  298.  
  299.   printf("  Intersect tests         : %-8ld\n", THEWORLD.intersect_tests);
  300.   printf("  Total Intersections     : %-8ld\n",(THEWORLD.ray_intersects+
  301.                                                 THEWORLD.bbox_intersects));
  302.   printf("     Object intersections : %-8ld\n", THEWORLD.ray_intersects);
  303.   printf("     Bbox   intersections : %-8ld\n", THEWORLD.bbox_intersects);
  304.   printf("  Rays traced             : %-8ld\n",(THEWORLD.primary_traced+
  305.                                                 THEWORLD.to_lamp+
  306.                                                 THEWORLD.refl_trans));
  307.   printf("     Primary              : %-8ld\n", THEWORLD.primary_traced);
  308.   printf("     To lamps             : %-8ld\n", THEWORLD.to_lamp);
  309.   printf("     Refl. or Trans.      : %-8ld\n", THEWORLD.refl_trans);
  310.   printf("  Pattern match checks    : %-8ld\n", THEWORLD.pattern_matches);
  311.  
  312.   printf("\n  Data sent to: %s\n\n",T